package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.PwsStatus;
import com.ruoyi.system.domain.TankInfo;
import com.ruoyi.system.domain.vo.PwsQueueAndPwsTableVo;
import com.ruoyi.system.service.PwsStatusService;
import com.ruoyi.system.service.PwsTableService;
import com.ruoyi.system.service.TankInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.PwsQueue;
import com.ruoyi.system.service.IPwsQueueService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2025-02-20
 */
@RestController
@Api(tags = "【PwsQueue】")
@RequestMapping("/system/queue")
public class PwsQueueController extends BaseController
{
    @Autowired
    private IPwsQueueService pwsQueueService;
    @Autowired
    private PwsStatusService pwsStatusService;
    @Autowired
    PwsTableService pwsTableService;
    @Autowired
    TankInfoService tankInfoService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:queue:list')")
    @PostMapping("/list")
    @ApiOperation(value = "【PwsQueue】列表")
    public TableDataInfo list(@RequestBody PwsQueue pwsQueue, @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize)
    {
        List<PwsQueue> list = pwsQueueService.selectPwsQueueList(pwsQueue, pageNo, pageSize);
        Long total = pwsQueueService.selectPwsQueueCount(pwsQueue);
        List<Map<String,Object>> mapList=new ArrayList<>();
        for (PwsQueue pwsQueues:list) {
            TankInfo tankInfo = tankInfoService.getTankInfoByLineNo((long)pwsQueues.getLineNo()).get(0);
            pwsQueues.setTankName(tankInfo.getTank());
            Map<String,Object> map=new HashMap<>();
            map.put("pwsQueue",pwsQueues);
            map.put("pwsTable",pwsTableService.getPwsTableByOrderNo(pwsQueues.getOrderNo()));
            mapList.add(map);
        }
        return getDataTable(mapList, total);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:queue:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "【PwsQueue】导出")
    public void export(HttpServletResponse response, PwsQueue pwsQueue)
    {
        List<PwsQueue> list = pwsQueueService.selectPwsQueueAll(pwsQueue);
        ExcelUtil<PwsQueue> util = new ExcelUtil<PwsQueue>(PwsQueue.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:queue:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "【PwsQueue】详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pwsQueueService.selectPwsQueueById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:queue:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation(value = "新增")
    public AjaxResult add(@RequestBody PwsQueue pwsQueue)
    {
        return toAjax(pwsQueueService.insertPwsQueue(pwsQueue));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:queue:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    @ApiOperation(value = "修改")
    public AjaxResult edit(@RequestBody PwsQueue pwsQueue)
    {
        return toAjax(pwsQueueService.updatePwsQueue(pwsQueue));
    }
    @PreAuthorize("@ss.hasPermi('system:queue:edit')")
    @Log(title = "投入生产修改改状态", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStatus")
    @ApiOperation(value = "投入生产修改改状态")
    public AjaxResult updateStatus(@RequestParam Long id)
    {
        return toAjax(pwsQueueService.updateStatus(id));
    }
    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:queue:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation(value = "删除")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pwsQueueService.deletePwsQueueByIds(ids));
    }


    @PreAuthorize("@ss.hasPermi('system:queue:add')")
    @GetMapping("/addPwsQueue")
    @ApiOperation(value = "下发生产单")
    public AjaxResult addPwsQueue(@RequestParam String orderNo,@RequestParam Integer lineNo,@RequestParam(value = "sequence",defaultValue = "0") Long sequence)
    {
        //pws_status表is_ready不为0时禁止操作
        PwsStatus pwsStatus = pwsStatusService.selectPwsStatusByOrderNo(orderNo,0);
        if(pwsStatus==null){
                return error("下发状态不为【未下发】时禁止操作");
        }else {

            boolean flag = pwsQueueService.addPwsQueue(orderNo, lineNo, sequence);

            return flag ?success("下发生产单成功"):error("下发生产单失败");
        }
    }

    @PreAuthorize("@ss.hasPermi('system:queue:query')")
    @GetMapping("/selectpwsQueueAndPwsTableByOrderNo")
    @ApiOperation(value = "按单号关联查询pws_queue，pws_table表中内容")
    public AjaxResult selectpwsQueueAndPwsTableByOrderNo(@RequestParam String order_no){
        PwsQueueAndPwsTableVo pwsQueueAndPwsTableVo = pwsQueueService.selectpwsQueueAndPwsTableByOrderNo(order_no);
        return success(pwsQueueAndPwsTableVo);
    }
    @PreAuthorize("@ss.hasPermi('system:queue:remove')")
    @GetMapping("/delPwsQueueByOrderNo")
    @ApiOperation(value = "删订单队列")
    public AjaxResult delPwsQueue(@RequestParam String order_no){
        //字段pws_status不为0时禁止操作
        PwsQueue pwsQueue = pwsQueueService.selectPwsQueueStatusByOrderNo(order_no,1);
        if(pwsQueue==null){
            return error("订单状态不为1时禁止操作");
        }else {
            boolean flag = pwsQueueService.delPwsQueueByOrderNo(order_no);
            return flag ? success("删订单队列成功") : error("删订单队列失败");
        }
    }
    @PreAuthorize("@ss.hasPermi('system:queue:edit')")
    @GetMapping("/updatePwsQueueByOrderNo")
    @ApiOperation(value = "修改队列顺序")
    public AjaxResult updatePwsQueue(@RequestParam String order_no,@RequestParam Long sequence){
        //字段pws_status不为0时禁止操作
        PwsQueue pwsQueue = pwsQueueService.selectPwsQueueStatusByOrderNo(order_no,1);
        if(pwsQueue==null){
            return error("字段pws_status不为1时禁止操作");
        }else {
            boolean flag = pwsQueueService.updatePwsQueueByOrderNo(order_no,sequence);
            return flag ? success("修改队列顺序成功") : error("修改队列顺序失败");
        }
    }
}
